package com.lotter.api.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lotter.api.service.BankCardService;
import com.lotter.api.service.PresentCheckService;
import com.lotter.api.service.UserAccountService;
import com.lotter.common.exception.LotterCusException;
import com.lotter.common.util.MD5Util;
import com.lotter.common.util.StringUtil;
import com.lotter.invocation.StaticDataContext;
import com.lotter.model.bankcard.BankCard;
import com.lotter.model.user.UserAccount;


/**
 * 取现
 * @author zhangzechi
 */
@Controller
@RequestMapping(value="/website/present")
public class PresentCheckController extends BaseController {
	
	@Autowired
	private PresentCheckService presentCheckService;
	
	@Autowired
	private BankCardService bankCardService;
	
	@Autowired
	private UserAccountService userAccountService ;
	
	
	
	/**
	 * 页面指向
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/view",method=RequestMethod.GET)
	public Map<String,Object> view(@RequestParam(value ="token",required = true) String token) {
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			Long uid = StaticDataContext.onLineUid(token);
			BankCard bankCard = bankCardService.findBank(uid);
			 if(bankCard == null){
		    	return ajaxLotterUser("你还没有绑卡，请首先进行绑卡");
		     }
			 UserAccount userAccount = userAccountService.selectByPrimaryKey(uid);
			 result.put("amount", userAccount.getAmount());
			 result.put("bankCard", bankCard);
		} catch (LotterCusException e) {
			logger.error("查询绑定银行卡出错");
			return ajaxLotterServer("服务器异常，请联系管理员");
		}
		return ajaxLotterSuccess(result);
	}
	
	
	/**
	 * 提现
	 * @param request
	 * @param presentamount
	 * @param payPwd
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/insert",method=RequestMethod.POST)
	public Map<String,Object> insert(@RequestParam(value ="token",required = true) String token,
		@RequestParam(value="presentamount",required= true) Long presentamount,
		@RequestParam(value="paypwd",required= true) String  paypwd){
		if(presentamount==null || presentamount.longValue() < 0){
			return ajaxLotterStage("提现金额有误,请检查后再试");
		}
		if(StringUtil.isBlank(paypwd)){
			return ajaxLotterStage("支付密码必须输入");
		}
		try {
			Long uid = StaticDataContext.onLineUid(token);
			UserAccount userAccount = userAccountService.selectByPrimaryKey(uid);
			
			BankCard bankCard = bankCardService.findBank(uid);
			 if(bankCard == null){
		    	return ajaxLotterUser("你还没有绑卡，请首先进行绑卡");
		     }
		     if(!MD5Util.md5Hex(paypwd).equals(bankCard.getPaypwd())){
		    	return ajaxLotterUser("支付密码不正确,请重新再试");
		     }
		     if(presentamount.longValue()  < 10000){
		    	return ajaxLotterUser("提现金额必须大于等于100");
		     }
		     if(presentamount.longValue()  > 5000000){
		    	return ajaxLotterUser("提现金额必须小于等于50000");
		     }
		     
		     
		     Map<String,String> result = new HashMap<String, String>();
		     //当天提现不超过三次
			 int number =  presentCheckService.withdrawalNum(userAccount.getId());
			 if(number > 3){
				 return ajaxLotterUser("当天提现次数超过3次 不能继续提现");
			 }
			//提现逻辑
			 Boolean bool = presentCheckService.insert(result, userAccount, presentamount, bankCard);
			 if(!bool){
				return ajaxLotterUser(result.get("message"));
			 } 
		 } catch (LotterCusException e1) {
			logger.error("取现异常");
			return ajaxLotterServer("服务器异常，请联系管理员");
		 }
         return ajaxLotterSuccess();	    
	}

}
